package com.bytech.admin.config;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.bytech.common.biz.intercept.BlogUserAgentIntercept;
import com.bytech.common.biz.intercept.BlogUserAgentMethodArgumentResolver;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.bytech.admin.biz.interceptor.AdminAuthInterceptor;

/**
 * 管理后台拦截器和参数注入器配置 会注入一个特定的 BeanName 缺省的微服务中的拦截器和controller入参注入器会根据这个BeanName决定是否需要配置
 *
 * @Author Zhao
 * @Create 2021/8/9 10:47
 */
@Configuration(value = "webMvcConfigurer")
public class AdminSpringMVCConfig implements WebMvcConfigurer {

    @Value("${interceptor.pathPatterns}")
    private String pathPatterns;
    @Value("${interceptor.excludePathPatterns}")
    private String excludePathPatterns;

    @Resource
    private AdminAuthInterceptor adminAuthInterceptor;
    // @Resource
    // private IdempotentInterceptor idempotentInterceptor;
    @Resource
    private BlogUserAgentIntercept blogUserAgentIntercept;

    @Override
    public void addInterceptors(InterceptorRegistry interceptor) {
        String[] pathPatternArr = pathPatterns.split(";");
        String[] excludePathPatternArr = excludePathPatterns.split(";");
        interceptor.addInterceptor(blogUserAgentIntercept)
                .addPathPatterns(pathPatternArr)
                .excludePathPatterns(excludePathPatternArr);
        interceptor.addInterceptor(adminAuthInterceptor)
                .addPathPatterns(pathPatternArr)
                .excludePathPatterns(excludePathPatternArr);
        // 测试用的幂等性拦截器
        // interceptor.addInterceptor(idempotentInterceptor)
        //         .addPathPatterns(pathPatternArr)
        //         .excludePathPatterns(excludePathPatternArr);
    }

    @Resource
    private BlogUserAgentMethodArgumentResolver blogUserAgentMethodArgumentResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(blogUserAgentMethodArgumentResolver);
    }

    @Bean
    public ErrorViewResolver errorViewResolver() throws IOException {
        return new BlogErrorViewResolver();
    }
}

/**
 * 简单处理HTTP请求异常页 可以参考spring默认的
 *
 * @see org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver
 */
class BlogErrorViewResolver implements ErrorViewResolver {

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        return new ModelAndView("/error/" + status.value());
    }
}
